#!/bin/sh
#------------------------------------------------------------------------------
# =========                 |
# \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
#  \\    /   O peration     |
#   \\  /    A nd           | www.openfoam.com
#    \\/     M anipulation  |
#------------------------------------------------------------------------------
#     Copyright (C) 2016-2020 OpenCFD Ltd.
#------------------------------------------------------------------------------
# License
#     This file is part of OpenFOAM, distributed under GPL-3.0-or-later.
#
# Script
#     makeVTK
#
# Description
#     Make and install VTK.
#     The VTK sources should be located under one of these locations:
#       - $WM_THIRD_PARTY_DIR/VTK-VERSION
#
#     To use the VTK source from the ParaView source tree, simply make an
#     appropriate link first. For example,
#
#         ln -s ParaView-v5.3.0/VTK VTK-7.1.0
#
# ----------------------------------------------
# NO USER-CONFIGURABLE SETTINGS WITHIN THIS FILE
#------------------------------------------------------------------------------
# Run from third-party directory only
cd "${0%/*}" || exit
wmakeCheckPwd "$WM_THIRD_PARTY_DIR" 2>/dev/null || {
    echo "Error (${0##*/}) : not located in \$WM_THIRD_PARTY_DIR"
    echo "    Check your OpenFOAM environment and installation"
    exit 1
}
. etc/tools/ThirdPartyFunctions
. etc/tools/ParaViewFunctions
. etc/tools/vtkFunctions
#------------------------------------------------------------------------------

unset vtk_version mesa_version                           # Purge current values

# vtk version from OpenFOAM etc/config.sh file:
_foamConfig vtk

VTK_VERSION="$vtk_version"

case "$VTK_VERSION" in
[Vv]*)
    VTK_VERSION="${VTK_VERSION##*-}"        # Without "VTK-" prefix
    ;;
esac
VTK_VERSION="${VTK_VERSION%%-*}"            # Without suffix (eg, -mesa)


# New rendering backend (starting with vtk 7?).
withGL2=auto # auto-config based on version

#------------------------------------------------------------------------------
usage() {
    exec 1>&2
    while [ "$#" -ge 1 ]; do echo "$1"; shift; done
    cat<<USAGE

usage: ${0##*/} [OPTION] [vtk-VERSION] [CMAKE-OPTION]
options:
  -gcc              Force use of gcc/g++
  -cmake PATH       with cmake from the given path
  -rebuild          for repeated builds (-make -install) *use with caution*
  -gl2              with new rendering backend (default: auto)
  -mesa             with mesa (if not already enabled)
  -mesa-prefix DIR  location of mesa installation
  -mesa-include DIR location of mesa headers (current: ${MESA_INCLUDE:-none})
  -mesa-lib PATH    path to mesa library     (current: ${MESA_LIBRARY:-none})
  -osmesa           with off-screen mesa only
  -mpi              with mpi (VTK_Group_MPI=ON)
  -mpi=N            with max 'N' mpi processes. N=0 for no upper-limit.
  -verbose          verbose output in Makefiles
  -version VER      specify an alternative version (current: $VTK_VERSION)
  -buildType NAME   specify the build type (default: Release)
  -suffix NAME      specify a suffix to distinguish the build
  -help

The -no-FEATURE option can be used to forcibly disable these features:
  -no-gl2 | -no-mesa | -no-mpi

CMake options start with a capital letter and contain an '='.
For example,
    ${0##*/} BUILD_TESTING=ON
to add tests and avoid building documentation

For finer control, the build stages can be selected or deselected individually:
  -patch     -no-patch
  -config    -no-config
  -make      -no-make
  -install   -no-install


* Make and install VTK-$VTK_VERSION located under
      \$WM_THIRD_PARTY_DIR/VTK-$VTK_VERSION
  ->  \$WM_THIRD_PARTY_DIR/platforms/$WM_ARCH$WM_COMPILER/VTK-$VTK_VERSION$BUILD_SUFFIX

USAGE
    # showDownloadHint VTK
    exit 1
}
#------------------------------------------------------------------------------
exportCompiler minimal  # Minimal compiler info for CMake/configure

# Various building stages
unset runPATCH runCONFIG runMAKE runINSTALL
runDEFAULT=true

# Parse options
while [ "$#" -gt 0 ]
do
    case "$1" in
    '') ;;      # Ignore empty
    -h | -help) usage   ;;
    -gcc)       useGcc  ;;

    [0-9]* | vtk-[0-9]* | VTK-[0-9]*) # VTK version
        setVtkVersion "${1%%/}"
        ;;
    [A-Z]*=*)            # cmake variables
        addCMakeVariable "$1"
        ;;
    -patch)              # stage 0: patch sources
        runPATCH=true
        unset runDEFAULT
        ;;
    -no-patch)
        runPATCH=false
        ;;
    -config)             # stage 1: config only
        runCONFIG=true
        unset runDEFAULT
        ;;
    -no-config)
        runCONFIG=false
        ;;
    -make)               # stage 2: make only
        runMAKE=true
        unset runDEFAULT
        ;;
    -no-make)
        runMAKE=false
        ;;
    -install)            # stage 3: install only
        runINSTALL=true
        unset runDEFAULT
        ;;
    -no-install)
        runINSTALL=false
        ;;
    -rebuild)            # shortcut for rebuilding
        runMAKE=true
        runINSTALL=true
        unset runDEFAULT
        ;;
    -gl2)
        withGL2=true
        ;;
    -no-gl2)
        withGL2=false
        ;;
    -mesa)
        withMESA=true
        ;;
    -osmesa)
        withMESA=true
        withOSMESA=true
        ;;
    -no-mesa)
        withMESA=false
        withOSMESA=false
        ;;
    -mesa-prefix)
        [ "$#" -ge 2 ] || die "'$1' option requires an argument"
        withMESA=true
        MESA_INCLUDE="${2%%/}/include"
        # Could be under (lib64 | lib)
        MESA_LIBRARY="${2%%/}/lib$WM_COMPILER_LIB_ARCH/libOSMesa.so"
        [ -f "$MESA_LIBRARY" ] || MESA_LIBRARY="${2%%/}/lib/libOSMesa.so"
        shift
        ;;
    -mesa-include)
        [ "$#" -ge 2 ] || die "'$1' option requires an argument"
        withMESA=true
        MESA_INCLUDE="${2%%/}"
        shift
        ;;
    -mesa-lib)
        [ "$#" -ge 2 ] || die "'$1' option requires an argument"
        withMESA=true
        MESA_LIBRARY="${2%%/}"
        shift
        ;;
    -mpi)
        withMPI=true
        ;;
    -mpi=[0-9]*) # mpi and max mpi processes
        withMPI=true
        MPI_MAX_PROCS="${1##*=}"
        ;;
    -no-mpi)
        withMPI=false
        ;;
    -cmake)
        [ "$#" -ge 2 ] || die "'$1' option requires an argument"
        CMAKE_PATH="${2%%/}"
        shift
        ;;
    -verbose)
        withVERBOSE=true
        ;;
    -version)
        [ "$#" -ge 2 ] || die "'$1' option requires an argument"
        setVtkVersion "${2%%/}"
        VTK_VERSION="${2%%/}"
        shift
        ;;
    -buildType)
        [ "$#" -ge 2 ] || die "'$1' option requires an argument"
        BUILD_TYPE="$2"
        shift
        ;;
    -suffix)
        [ "$#" -ge 2 ] || die "'$1' option requires an argument"
        setBuildSuffix "$2"
        shift
        ;;
    *)
        die "unknown option/argument: '$1'"
        ;;
    esac
    shift
done

[ -n "$VTK_VERSION" ] || die "The vtk-VERSION was not specified"

# Version-specific adjustments
if [ "$withGL2" = auto ]
then
    if [ "${VTK_VERSION%%.*}" -lt 7 ]
    then
        withGL2=false
    else
        withGL2=true
    fi
fi


if [ "$runDEFAULT" = true ]
then
    : ${runPATCH:=true}
    : ${runCONFIG:=true}
    : ${runMAKE:=true}
    : ${runINSTALL:=true}
fi


# Set configure options
#~~~~~~~~~~~~~~~~~~~~~~
addVerbosity        # verbose makefiles
addMpiSupport       # set MPI-specific options
addMesaSupport      # set MESA-specific options
addGL2Support       # new rendering backend

# Set off-screen options
if [ "$withOSMESA" = true ]
then
    addCMakeVariable "VTK_USE_X=OFF"
    addCMakeVariable "OPENGL_INCLUDE_DIR=$MESA_INCLUDE"
    addCMakeVariable "OPENGL_gl_LIBRARY=$MESA_LIBRARY"
    addCMakeVariable "OPENGL_glu_LIBRARY=$MESA_LIBRARY"
    addCMakeVariable "OPENGL_xmesa_INCLUDE_DIR=$MESA_INCLUDE"
fi

setVtkDirs          # where things are or should be put


# Build and install
# ~~~~~~~~~~~~~~~~~
cat<<SUMMARY

Build stages selected
---------------------
    -patch    ${runPATCH:-false}
    -config   ${runCONFIG:-false}
    -make     ${runMAKE:-false}
    -install  ${runINSTALL:-false}
---------------------
Features selected
    mesa      ${withMESA:-false}
    mpi       ${withMPI:-false}
---------------------
Compiler
    cxx       ${CXX:-unknown}
    cxxflags  ${CXXFLAGS:-none}
---------------------
Version information
    vtk       ${VTK_VERSION:-unknown}
    build     ${BUILD_TYPE:-unknown}
---------------------
SUMMARY

[ "$runPATCH"   = true ] && patchVTK
[ "$runCONFIG"  = true ] && configVTK
[ "$runMAKE"    = true ] && makeVTK
[ "$runINSTALL" = true ] && installVTK

echo
echo Done

#------------------------------------------------------------------------------
